﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenMetaverse;

namespace Trim
{
    public partial class IMForm : Form
    {
        private UUID IMID;
        private string IMName;
        private bool Group;

        private bool IsActive = false;

        private bool IconBright = false;

        public IMForm(UUID imid, string imname, bool group)
        {
            IMID = imid;
            IMName = imname;
            Group = group;
            InitializeComponent();
        }

        private void IMForm_Load(object sender, EventArgs e)
        {
            this.Text = "Conversation with " + IMName;
            if (Group)
            {
                AddMessage("Joining group IM session...");
                Program.client.SL.Self.OnGroupChatJoin += delegate(UUID groupChatSessionID, string sessionName, UUID tmpSessionID, bool success)
                {
                    BeginInvoke((MethodInvoker)delegate
                    {
                        AddMessage(success ? "Joined session." : "Failed to join session.");
                    });
                };
                Program.client.SL.Self.RequestJoinGroupChat(IMID);
            }
        }

        private void IMForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Program.client.IMForms.ContainsKey(IMID))
                Program.client.IMForms.Remove(IMID);
        }

        public void AddMessage(string message)
        {
            HistoryBox.AppendText("[" + DateTime.Now.ToString("hh:mm") + "] " + message);
            HistoryBox.AppendText(Environment.NewLine);
            //HistoryBox.Select(HistoryBox.Text.Length, 0);

            if (!this.IsActive)
            {
                IconFlasher.Enabled = true;
            }
        }

        private void ComposeBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (Group)
                {
                    Program.client.SL.Self.InstantMessageGroup(IMID, ComposeBox.Text);
                }
                else
                {
                    Program.client.SL.Self.InstantMessage(IMID, ComposeBox.Text);
                    AddMessage("You: " + ComposeBox.Text);
                }

                ComposeBox.Text = "";
                e.Handled = true;
            }
        }

        private void IMForm_Activated(object sender, EventArgs e)
        {
            this.IsActive = true;
            this.IconBright = false;
            this.Icon = Properties.Resources.Chat;
            IconFlasher.Enabled = false;
        }

        private void IMForm_Deactivate(object sender, EventArgs e)
        {
            this.IsActive = false;
        }

        private void IconFlasher_Tick(object sender, EventArgs e)
        {
            IconBright = !IconBright;
            if (IconBright)
                this.Icon = Properties.Resources.ChatBright;
            else
                this.Icon = Properties.Resources.Chat;
        }
    }
}
